package cn.iocoder.yudao.module.statistics.controller.app.shareprofit;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.statistics.controller.app.shareprofit.vo.AppStoreTradeShareProfitPageReqVO;
import cn.iocoder.yudao.module.statistics.controller.app.shareprofit.vo.AppStoreTradeShareProfitRespVO;
import cn.iocoder.yudao.module.statistics.dal.dataobject.shareprofit.StoreTradeShareProfitDO;
import cn.iocoder.yudao.module.statistics.service.shareprofit.StoreTradeShareProfitService;
import cn.iocoder.yudao.module.system.api.partner.PartnerApi;
import cn.iocoder.yudao.module.system.api.partner.dto.PartnerRespDTO;
import cn.iocoder.yudao.module.system.enums.partner.PartnerTypeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "APP用户 - 合伙人收益")
@RestController
@RequestMapping("/statistics/share-profit")
@Validated
public class AppStoreTradeShareProfitController {

    @Resource
    private StoreTradeShareProfitService storeTradeShareProfitService;

    @Resource
    private PartnerApi partnerApi;

    @GetMapping("/queryShareProfit")
    @Operation(summary = "收益数据")
    public CommonResult<Map<String, Long>> queryShareProfit() {
        Long userId = getLoginUserId();
        return success(storeTradeShareProfitService.queryShareProfit(userId));
    }

    @GetMapping("/queryShareProfitList")
    @Operation(summary = "收益明细")
    public CommonResult<PageResult<AppStoreTradeShareProfitRespVO>> queryShareProfitList(AppStoreTradeShareProfitPageReqVO pageReqVO) {
        pageReqVO.setUserId(getLoginUserId());
        if (Objects.nonNull(pageReqVO.getPartnerType()) && 1 == pageReqVO.getQueryType()) {
            pageReqVO.setReportDate(LocalDate.now().minusDays(1));
        }
        PageResult<StoreTradeShareProfitDO> pageResult = storeTradeShareProfitService.queryDetailList(pageReqVO);
        PageResult<AppStoreTradeShareProfitRespVO> pageResultVo = BeanUtils.toBean(pageResult, AppStoreTradeShareProfitRespVO.class);
        pageResultVo.getList().forEach(item -> {
            item.setShareProfitPayPrice(item.getShareProfitPayPrice()-item.getShareProfitRefundPrice());
            item.setPartnerTypeDesc(PartnerTypeEnum.getNameByType(item.getPartnerType()));
        });
        return success(pageResultVo);
    }

    @GetMapping("/getShareProfitDetail")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @Operation(summary = "收益详情")
    public CommonResult<AppStoreTradeShareProfitRespVO> getShareProfitDetail(@RequestParam("id") Long id) {
        StoreTradeShareProfitDO storeTradeShareProfit = storeTradeShareProfitService.getStoreTradeShareProfit(id);
        AppStoreTradeShareProfitRespVO storeTradeShareProfitRespVO = BeanUtils.toBean(storeTradeShareProfit, AppStoreTradeShareProfitRespVO.class);
        storeTradeShareProfitRespVO.setShareProfitPayPrice(storeTradeShareProfitRespVO.getShareProfitPayPrice()-storeTradeShareProfitRespVO.getShareProfitRefundPrice());
        storeTradeShareProfitRespVO.setPartnerTypeDesc(PartnerTypeEnum.getNameByType(storeTradeShareProfitRespVO.getPartnerType()));

        List<PartnerRespDTO> partnerList = partnerApi.getListByIds(Arrays.asList(storeTradeShareProfit.getPartnerId()));
        if (!CollectionUtils.isAnyEmpty(partnerList)) {
            PartnerRespDTO partnerRespDTO = partnerList.get(0);
            storeTradeShareProfitRespVO.setPartnerName(partnerRespDTO.getName());
            storeTradeShareProfitRespVO.setPhone(partnerRespDTO.getPhone());
        }
        return success(storeTradeShareProfitRespVO);
    }

}